<?php

include_once 'common_model.php';		//加载数据库操作类
include_once dirname(__DIR__).'/sms_model.php';		//加载短信类
//include_once dirname(__DIR__).'/JWT.php';
class Account_Model extends Account_Common_Model
{
	public function __construct()
	{
		header('Content-Type: text/html; charset=utf-8'); 
		parent::__construct();
	}
	
	/************************************************
					common function
	*************************************************/
    /*
        获取用户信息
    */
    public function getUserInfoByMobPWD($arrData)
    {
        $timestamp = time();
        $result = array();

        $dealerDB = Game_CONST::DBConst_Name;
        $account_where = 'password="'.$arrData['password'].'" and phone="'.$arrData['mobile'].'"';
        $account_sql = 'select open_id,account_id,nickname,headimgurl,phone,recommend_code,is_manage_on,my_code from '.WX_Account.' where '.$account_where.'';
        $account_query = $this->getDataBySql($dealerDB,1,$account_sql);

        if(DB_CONST::DATA_NONEXISTENT == $account_query)
        {
            log_message('error', "function(getUserInfo):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"账号不存在");
        }

        $user_code = isset($arrData['user_code']) ? $arrData['user_code'] : "";

        $bind_request['user_code'] = $user_code;
        $bind_request['account_id'] = $account_query['account_id'];
        $my_user_code = $this->bindUserCode($bind_request);

        //$this->tryJoinGuild($bind_request);

        $result['account_id'] = $account_query['account_id'];
        $result['nickname'] =  $account_query['nickname'];;
        $result['phone'] = $account_query['phone'];
        $result['user_code'] = $my_user_code;
        $result['open_id'] =  $account_query['open_id'];
//        $result['token'] = JWT::encode($result);
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取用户信息");
    }
    public function chongzhiData($outTradeNo){
        $dealerDB = Game_CONST::DBConst_Name;
        return $this->getDataBySql($dealerDB, 1, 'select * from chongzhi where outTradeNo="'.$outTradeNo.'"');
    }
    public function chongzhiLog($data){
        $dealerDB = Game_CONST::DBConst_Name;
        return $this->getInsertID($dealerDB, 'chongzhi', $data);
    }
    public function chongzhiUpdate($data){
        $dealerDB = Game_CONST::DBConst_Name;
        return $this->updateFunc($dealerDB, 'outTradeNo',$data['outTradeNo'],'chongzhi', $data);
    }

    /************************************************
    common function
     *************************************************/
    /*
        获取用户信息
    */
    public function getUserInfoByMobSMS($arrData)
    {
        $timestamp = time();
        $result = array();

//        $smsInfo =  $this->checkSmsCodeLogin($arrData);
//
//        if ($smsInfo['result'] != 0){
//            return $smsInfo;
//        }

        $dealerDB = Game_CONST::DBConst_Name;
        $account_where = 'phone="'.$arrData['mobile'].'"';
        $account_sql = 'select open_id,account_id,nickname,phone,my_code from '.WX_Account.' where '.$account_where.'';
        $account_query = $this->getDataBySql($dealerDB,1,$account_sql);

        if(DB_CONST::DATA_NONEXISTENT == $account_query)
        {
            log_message('error', "function(getUserInfo):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"账号不存在");
        }


        $user_code = isset($arrData['user_code']) ? $arrData['user_code'] : "";

        $bind_request['user_code'] = $user_code;
        $bind_request['account_id'] = $account_query['account_id'];
        $my_user_code = $this->bindUserCode($bind_request);

        //$this->tryJoinGuild($bind_request);

        $result['account_id'] = $account_query['account_id'];
        $result['nickname'] =  $account_query['nickname'];;
        $result['phone'] = $account_query['phone'];
        $result['user_code'] = $my_user_code;
        $result['open_id'] =  $account_query['open_id'];
//        $result['token'] = JWT::encode($result);
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取用户信息");
    }
    public function getUserByAccountID($account_id)
    {
        $result = $this->getDataBySql("dealer_2", 1, 'select * from '.WX_Account.' where account_id = '.$account_id);
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取用户");
    }
    public function getUserListByAccountID($arrData)
    {
        $account_where = "account_id in (".implode(",",$arrData).")";
        $result = $this->getDataBySql("dealer_2", 0, 'select account_id,nickname,headimgurl from '.WX_Account.' where '.$account_where.'');
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取用户列表");
    }
    //-------------
    public function choujiangSetting(){
        $dealerDB = Game_CONST::DBConst_Name;
        return $this->getDataBySql($dealerDB, 0, 'select * from  choujiang_setting order by id');
    }

    public function choujiangLog($data){
        $timestamp = time();
        $dealerDB = Game_CONST::DBConst_Name;
//        $arrData['journal_type'] = Game_CONST::JournalType_Income;	//1入账，2出账
//        $arrData['account_id']=$data['account_id'];
//        $arrData['object_type']=Game_CONST::ObjectType_Luckdraw;
//        $arrData['object_id']=0;
//        $arrData['ticket_count']=$data['ticket_count'];
//        $arrData['extra']="";
        // 增加房卡记录
//        $this->updateRoomTicketJournal($arrData,$dealerDB);
        // 增加抽奖记录
        $this->getInsertID($dealerDB, 'choujiang_journal', $data);

        //增加自己账户上的房卡
//        $updateTicket_str = 'update_time='.$timestamp.',update_appid="aid_'.$data['account_id'].'",ticket_count=ticket_count+'.$data['ticket_count'];
//        $updateTicket_where = 'account_id='.$data['account_id'];
//        $this->changeNodeValue($dealerDB,Room_Ticket,$updateTicket_str,$updateTicket_where);
        // 减少抽奖次数
        $this->updateFunc($dealerDB, 'account_id', $data['account_id'], "wechat_account",["choujiang_count"=>$data['chou_balance']]);

        return true;
    }

    public function choujiangJournal($account_id,$limit){
        $dealerDB = Game_CONST::DBConst_Name;
        return $this->getDataBySql($dealerDB, 0, 'select * from  choujiang_journal where account_id = '.$account_id.' and type = 11 order by id desc limit '.$limit);
    }
    public function choujiangWeekTicket($account_id,$yearweek){
        $dealerDB = Game_CONST::DBConst_Name;
        return $this->getDataBySql($dealerDB, 1, 'select * from  t_stat_week_roomcard where account_id = '.$account_id.' and yearweek = '.$yearweek.' order by id desc ');
    }
    public function choujiangMouthTicket($account_id){
        $dealerDB = Game_CONST::DBConst_Name;
        return $this->getDataBySql($dealerDB, 1, 'select sum(ticket_uses) as ticket_uses from  t_stat_day_roomcard where account_id = '.$account_id.' and yearday like "'.date("Y-m").'%"  order by id desc ');
    }

    public function sendAccountRoomCard($arrData){
//        echo "<pre>";
        $result = array();
        $timestamp = time();
        $dealerDB = Game_CONST::DBConst_Name;
        $account_id = $arrData['account_id'];
        $adjust_count = $arrData['count'];
        if($adjust_count <= 0) {
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"房卡数量必须大于0");
        }
        //将房卡添加到自己账户
        $updateTicket_str = 'update_time='.$timestamp.',update_appid="aid_'.$account_id.'",ticket_count=ticket_count+'.$adjust_count;
        $updateTicket_where = 'account_id='.$account_id;
        $updateTicket_query = $this->changeNodeValue($dealerDB,Room_Ticket,$updateTicket_str,$updateTicket_where);

        //添加房卡流水
        $jounal_array['journal_type'] = Game_CONST::JournalType_Income;
        $jounal_array['account_id'] = $account_id;
        $jounal_array['object_type'] = Game_CONST::ObjectType_Recharge;
        $jounal_array['object_id'] = 0;
        $jounal_array['ticket_count'] = $adjust_count;
        $jounal_array['extra'] = "";
        $this->updateRoomTicketJournal($jounal_array,$dealerDB);
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"购买房卡成功");

    }
    /*
        获取用户信息
    */
	public function getUserInfo($arrData)
	{
		$timestamp = time();
		$result = array();

		if(!isset($arrData['open_id']) || $arrData['open_id'] == G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(getUserInfo):lack of open_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("open_id");
		}

		$open_id = $arrData['open_id'];

        $dealerDB = Game_CONST::DBConst_Name;
        //判断是否是unionid
//        if(substr($open_id, 0,2) === 'oP')
//        {
//            //判断open_id是否存在
//            $account_where = 'union_id="'.$open_id.'"';
//            $account_sql = 'select account_id,nickname,headimgurl,phone ,recommend_code,is_manage_on,my_code from '.WX_Account.' where '.$account_where.'';
//            $account_query = $this->getDataBySql($dealerDB,1,$account_sql);
//        }
//        else
//        {
//            //判断open_id是否存在
//            $account_where = 'open_id="'.$open_id.'"';
//            $account_sql = 'select account_id,nickname,headimgurl,phone ,recommend_code,is_manage_on,my_code from '.WX_Account.' where '.$account_where.'';
//            $account_query = $this->getDataBySql($dealerDB,1,$account_sql);
//        }
        $account_where = 'open_id="'.$open_id.'" or union_id="'.$open_id.'"';
        $account_sql = 'select account_id,nickname,headimgurl,phone ,recommend_code,is_manage_on,my_code,is_chongzhi,choujiang_count from '.WX_Account.' where '.$account_where.'';
        $account_query = $this->getDataBySql($dealerDB,1,$account_sql);

		if(DB_CONST::DATA_NONEXISTENT == $account_query)
		{
			log_message('error', "function(getUserInfo):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
			return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"账号不存在");
		}
		else
		{
		    $result = $account_query;
			$nickname =  addslashes($account_query['nickname']);
			$phone = $account_query['phone'] ? $account_query['phone'] : "";

		}

		$user_code = isset($arrData['user_code']) ? $arrData['user_code'] : "";

		$bind_request['user_code'] = $user_code;
		$bind_request['account_id'] = $result['account_id'];
		$my_user_code = $this->bindUserCode($bind_request);

		//$this->tryJoinGuild($bind_request);

		$result['nickname'] = $nickname;
		$result['open_id'] = $open_id;
		$result['phone'] = $phone;
		$result['user_code'] = $my_user_code;

		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取用户信息");
	}

    /*
    统计代理好友数量
    */
    public function getCountDlNumbers($user_code)
    {
        $timestamp = time();
        $result = array();

        if($user_code == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getCountDlNumbers):lack of user_code"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("user_code");
        }

        $account_where = 'recommend_code="'.$user_code.'"';
        $account_sql = 'select COUNT(account_id) sumCount from '.WX_Account.' where '.$account_where.'';
        $dealerDB = Game_CONST::DBConst_Name;
        $account_query = $this->getDataBySql($dealerDB,1,$account_sql);
        if(DB_CONST::DATA_NONEXISTENT == $account_query)
        {
            log_message('error', "function(getCountDlNumbers):lack of user_code"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"账号不存在");
        }
        $result['account'] = $account_query['sumCount'];
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"统计代理好友数量");
    }

    /*
        获取用户信息根据 recommend_code
    */
    public function getUserInfoByRecommend_Code($recommend_code,$page,$rows)
    {
        //获取页数,判断是否符合要求
        if($page <=0 )
        {
            $page = 1;
        }
        if($rows <=0 )
        {
            $rows = 10;
        }
        $result = array();
        $account_where = 'recommend_code="'.$recommend_code.'"  limit $page,$rows ';
        $account_sql = 'select account_id,nickname,headimgurl,phone,recommend_code from '.WX_Account.' where '.$account_where.'';
        $dealerDB = Game_CONST::DBConst_Name;
        $account_query = $this->getDataBySql($dealerDB,1,$account_sql);
        if(DB_CONST::DATA_NONEXISTENT == $account_query)
        {
            log_message('error', "function(getUserInfoByRecommend_Code):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"账号不存在");
        }
        else
        {
            $result['account_id'] = $account_query['account_id'];
            $result['nickname'] = $account_query['nickname'];
            $result['headimgurl'] = $account_query['headimgurl'];
            $result['phone'] = $account_query['phone'];
            $result['recommend_code'] = $account_query['recommend_code'];
        }
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取用户我的团队信息");
    }


    /*
        我的团队列表
        参数：user_code : user_code
        返回结果：
    */
    public function getMyTeamList($arrData)
    {
        $timestamp = time();
        $result = array();

        if(!isset($arrData['user_code']) || $arrData['user_code'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getMyTeamList):lack of user_code"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("code");
        }
        if(!isset($arrData['page']) || $arrData['page'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getMyTeamList):lack of page"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("page");
        }

        $dealerDB = Game_CONST::DBConst_Name;

        $user_code = $arrData['user_code'];
        $page = $arrData['page'];
        $dl = $arrData['dl'];

        $limit = 10;
        $offset = ($page - 1) * $limit;

        $sum_page = 1;
        $account_where = '';
        $account_sql = '';
        $count_sql = '';
        //获取团队成员记录
        if($dl)
        {
            //查询所有下线
            $account_where = 'recommend_code="'.$user_code.'" and p.level = 1 ';
            $count_sql = 'select count(a.account_id) as sum_count from '.WX_Account.' a left join t_proxy p on a.user_code=p.user_code where '.$account_where;
            //只查询下线代理
            $account_sql = 'select a.account_id,a.nickname,a.headimgurl,a.phone,a.recommend_code,a.create_time,a.user_code from '.WX_Account.' a left join t_proxy p on a.user_code=p.user_code where '.$account_where.' order by create_time desc limit '.$offset.','.$limit;
        }
        else
        {
            //查询所有下线
            $account_where = 'recommend_code="'.$user_code.'"';
            $account_sql = 'select account_id,nickname,headimgurl,phone,recommend_code,create_time,user_code from '.WX_Account.' where '.$account_where.' order by create_time desc limit '.$offset.','.$limit;
            $count_sql = 'select count(account_id) as sum_count from '.WX_Account.' where '.$account_where;
        }


        $redenvelop_query = $this->getDataBySql($dealerDB,0,$account_sql);
        if(DB_CONST::DATA_NONEXISTENT != $redenvelop_query)
        {
            $count_query = $this->getDataBySql($dealerDB,1,$count_sql);
            $sum_count = $count_query['sum_count'];
            $sum_page = ceil($sum_count/$limit);
            foreach($redenvelop_query as $item)
            {
                $array['account_id'] = $item['account_id'];
                $array['nickname'] = $item['nickname'];
                $array['headimgurl'] = $item['headimgurl'];
                $array['create_time'] = $item['create_time'];
                $array['user_code'] = $item['user_code'];
                $result[] = $array;
            }
        }

        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"我的团队成员列表","sum_page"=>$sum_page);
    }



    /*
    获取用户信息根据 account_id
    */
    public function getUserInfoByAccount_id($user_code)
    {
        $timestamp = time();
        $result = array();

        //判断open_id是否存在
        $account_where = 'user_code="'.$user_code.'"';
        $account_sql = 'select account_id,nickname,headimgurl,phone,recommend_code from '.WX_Account.' where '.$account_where.'';
        $dealerDB = Game_CONST::DBConst_Name;
        $account_query = $this->getDataBySql($dealerDB,1,$account_sql);
        if(DB_CONST::DATA_NONEXISTENT == $account_query)
        {
            log_message('error', "function(getUserInfoByAccount_id):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"账号不存在");
        }
        else
        {
            $result['account_id'] = $account_query['account_id'];
            $result['nickname'] = $account_query['nickname'];
            $result['headimgurl'] = $account_query['headimgurl'];
            $result['phone'] = $account_query['phone'];
            $result['recommend_code'] = $account_query['recommend_code'];
        }
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取用户信息");
    }

    /*
        获取代理信息
    */
    public function getUserInfoDl($user_code)
    {
        $timestamp = time();
        $result = array();

        //判断open_id是否存在
        $account_where = 'user_code="'.$user_code.'"';
        $account_sql = 'select id,account_id,user_code,level,sumPay,balance,increase,totalOffline,status,memo from '.T_Proxy.' where '.$account_where.'';
        $dealerDB = Game_CONST::DBConst_Name;
        $account_query = $this->getDataBySql($dealerDB,1,$account_sql);
        if(DB_CONST::DATA_NONEXISTENT == $account_query)
        {
            log_message('error', "function(getUserInfoDl):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"代理账号不存在");
        }
        else
        {
            $result['id'] = $account_query['id'];
            $result['account_id'] = $account_query['account_id'];
            $result['user_code'] = $account_query['user_code'];
            $result['level'] = $account_query['level'];
            $result['sumPay'] = $account_query['sumPay'];
            $result['balance'] = $account_query['balance'];
            $result['totalOffline'] = $account_query['totalOffline'];
            $result['status'] = $account_query['status'];
            $result['memo'] = $account_query['memo'];
            $result['increase'] = $account_query['increase'];
        }
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取代理信息");
    }


	/*
		获取用户信息
	*/
	protected function bindUserCode($arrData)
	{
		$timestamp = time();
		$user_code = $arrData['user_code'];
		$account_id = $arrData['account_id'];
		$dealerDB = Game_CONST::DBConst_Name;

		$my_user_code = "";
		//获取用户user_code
		$dist_where = 'account_id="'.$account_id.'" and is_delete=0';
		$dist_sql = 'select user_code,recommend_code from '.WX_Account.' where '.$dist_where.'';
		$account_query = $this->getDataBySql($dealerDB,1,$dist_sql);
		if(DB_CONST::DATA_NONEXISTENT != $account_query)	//老用户，直接取分销码
		{
			$my_user_code = $account_query['user_code'];
			$recommend_code = $account_query['recommend_code'];
			/**
			if($recommend_code == -1){
				if($user_code > 0){
					$update_str = 'recommend_code="'.$user_code.'",update_time='.$timestamp;
					$update_where = 'account_id='.$account_id;
					$update_query = $this->changeNodeValue($dealerDB,WX_Account,$update_str,$update_where);
				}
			}
             */
		} else {	
			log_message('error', "function(bindUserCode):account not exist"." in file".__FILE__." on Line ".__LINE__);
		}
		return $my_user_code;
	}

	/*
		尝试加入加入公会
	*/
	public function tryJoinGuild($arrData)
	{
		$timestamp = time();
		$user_code = $arrData['user_code'];		
		$account_id = $arrData['account_id'];
        $dealerDB = Game_CONST::DBConst_Name;	

		//获取邀请函公会账号信息
		$member_where = 'code="'.$user_code.'" and level=2 and is_delete=0';
		$member_sql = 'select member_id,account_id,group_id,level from '.Guild_Member.' where '.$member_where.' limit 1';
		$member_query = $this->getDataBySql($dealerDB,1,$member_sql);
		if(DB_CONST::DATA_NONEXISTENT == $member_query)
		{
			return false; //不是会长直接返回
		}
		$group_id = $member_query['group_id'];

		//获取用户是否加入公会
		$join_where = 'account_id='.$account_id.' and is_delete=0';
		$join_sql = 'select member_id from '.Guild_Member.' where '.$join_where.' limit 1';
		$join_query = $this->getDataBySql($dealerDB,1,$join_sql);
		if(DB_CONST::DATA_NONEXISTENT == $join_query)  //未加入公会的用户
		{
			$insert_array['create_time'] = $timestamp;
			$insert_array['create_appid'] = "aid_".$account_id;
			$insert_array['update_time'] = $timestamp;
			$insert_array['update_appid'] = "aid_".$account_id;
			$insert_array['is_delete'] = 0;
			$insert_array['account_id'] = $account_id;
			$insert_array['group_id'] = $group_id;
			$insert_array['level'] = 0;
			$insert_array['vice_president'] = -1;
			$insert_array['code'] = -1;
			$member_id = $this->getInsertID($dealerDB,Guild_Member, $insert_array);
			return true;
		}
	}

	public function httpGet($url) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_TIMEOUT, 500);
		// 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
		// 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($curl, CURLOPT_URL, $url);
		
		$res = curl_exec($curl);
		curl_close($curl);
		
		return $res;
	}
	
	

	
	public function getTicketGoodsList()
	{
		$timestamp = time();
		$result = array();

		$dealerDB = Game_CONST::DBConst_Name;
		
		//获取商品明细
		$goods_where = 'is_delete='.G_CONST::IS_FALSE;
		$goods_sql = 'select goods_id,title,price,ticket_count from '.Payment_Goods.' where '.$goods_where.' order by ticket_count asc';
		$goods_query = $this->getDataBySql($dealerDB,0,$goods_sql);
		if($goods_query != DB_CONST::DATA_NONEXISTENT)
		{
			$result = $goods_query;
			
		}
		
		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取商品列表");
	}

	public function getScoreStatistics($arrData)
	{
		$timestamp = time();
		$result = array();

		if(!isset($arrData['account_id']) || $arrData['account_id'] == G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(getScoreStatistics):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("account_id");
		}
		if(!isset($arrData['from']) )
		{
			log_message('error', "function(getScoreStatistics):lack of from"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("from");
		}
		if(!isset($arrData['to']) || $arrData['to'] == G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(getScoreStatistics):lack of to"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("to");
		}
	
		$dealerDB = Game_CONST::DBConst_Name;


		$account_id = $arrData['account_id'];
		$from_timestamp = $arrData['from'];
		$to_timestamp = $arrData['to'];

		$account_id = $arrData['account_id'];
		$from = $arrData['from'];
		$to = $arrData['to'];

		$from_timestamp = 0;
		if($from != "")
		{
			$from_timestamp = strtotime($from);
		}
		$to_timestamp = strtotime($to)+86399;

		//游戏类型 ：1炸金花  2斗地主  3梭哈  4德州  5斗牛 6广东麻将  
		$type_array = array("1"=>0,"2"=>0,"5"=>0,"6"=>0,"9"=>0);
		$sql = 'select game_type,board from '.Room_Scoreboard.' where create_time>='.$from_timestamp.' and create_time<='.$to_timestamp.' and board like "%\"'.$account_id.'\":%" and is_delete=0';
		$query = $this->getDataBySql($dealerDB,0,$sql);
		if($query != DB_CONST::DATA_NONEXISTENT)
		{
			foreach($query as $item)
			{
				$game_type = $item['game_type'];

				if(!isset($type_array[$game_type]))
				{
					$type_array[$game_type] = 0;
				}

				$board_ary = json_decode($item['board'],true);
				if(is_array($board_ary) && isset($board_ary[$account_id]))
				{
					$type_array[$game_type] += $board_ary[$account_id];
				}
			}
		}

		foreach($type_array as $game_type=>$score)
		{
			$result[] = array("game_type"=>$game_type,"score"=>$score);
		}

		
		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取积分列表");
	}

    public function getGameScore($arrData)
    {
        $result = array();
        if(!isset($arrData['account_id']) || $arrData['account_id'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getGameScore):lack of account_id "." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("account_id");
        }
        if(!isset($arrData['game_type']) )
        {
            log_message('error', "function(getGameScore):lack of game_type "." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("game_type");
        }
        $dealerDB = Game_CONST::DBConst_Name;

        $account_id = $arrData['account_id'];
        $game_type = $arrData['game_type'];
        $page = $arrData['page'];
        $rows = $arrData['rows'];
        $sql = '';
        switch ($game_type){
            case 52:
            case 56:
            case 58:
            case 59:
            case 55:
                $sql = 'select rgs.game_type,rgs.score,rgs.room_id,rgs.game_time,br.room_number from '.Room_Game_Score.
                    ' rgs left join bull_room br on rgs.room_id=br.room_id where rgs.account_id='.$account_id.' and rgs.game_type='.$game_type.'  order by rgs.game_time desc  limit '.$page.','.$rows;
                break;
            case 53:
                $sql = 'select rgs.game_type,rgs.score,rgs.room_id,rgs.game_time,br.room_number from '.Room_Game_Score.
                    ' rgs left join laibull_room br on rgs.room_id=br.room_id where rgs.account_id='.$account_id.' and rgs.game_type='.$game_type.'  order by rgs.game_time desc  limit '.$page.','.$rows;
                break;
            case 30:
            case 29:
            case 26:
            case 25:
            case 24:
                $sql = 'select rgs.game_type,rgs.score,rgs.room_id,rgs.game_time,br.room_number from '.Room_Game_Score.
                    ' rgs left join flower_room br on rgs.room_id=br.room_id where rgs.account_id='.$account_id.' and rgs.game_type='.$game_type.'  order by rgs.game_time desc  limit '.$page.','.$rows;
                break;
            case 20:
                $sql = 'select rgs.game_type,rgs.score,rgs.room_id,rgs.game_time,br.room_number from '.Room_Game_Score.
                    ' rgs left join dcx_room br on rgs.room_id=br.room_id where rgs.account_id='.$account_id.' and rgs.game_type='.$game_type.'  order by rgs.game_time desc  limit '.$page.','.$rows;
                break;
            case 19:
                $sql = 'select rgs.game_type,rgs.score,rgs.room_id,rgs.game_time,br.room_number from '.Room_Game_Score.
                    ' rgs left join paijiu_room br on rgs.room_id=br.room_id where rgs.account_id='.$account_id.' and rgs.game_type='.$game_type.'  order by rgs.game_time desc  limit '.$page.','.$rows;
                break;
            case 6:
                $sql = 'select rgs.game_type,rgs.score,rgs.room_id,rgs.game_time,br.room_number from '.Room_Game_Score.
                    ' rgs left join gd_mahjong_room br on rgs.room_id=br.room_id where rgs.account_id='.$account_id.' and rgs.game_type='.$game_type.'  order by rgs.game_time desc  limit '.$page.','.$rows;
                break;
            case 8:
                $sql = 'select rgs.game_type,rgs.score,rgs.room_id,rgs.game_time,br.room_number from '.Room_Game_Score.
                    ' rgs left join eba_room br on rgs.room_id=br.room_id where rgs.account_id='.$account_id.' and rgs.game_type='.$game_type.'  order by rgs.game_time desc  limit '.$page.','.$rows;
                break;
            case 2:
                $sql = 'select rgs.game_type,rgs.score,rgs.room_id,rgs.game_time,br.room_number from '.Room_Game_Score.
                    ' rgs left join landlord_room br on rgs.room_id=br.room_id where rgs.account_id='.$account_id.' and rgs.game_type='.$game_type.'  order by rgs.game_time desc  limit '.$page.','.$rows;
                break;
            case 21:
                $sql = 'select rgs.game_type,rgs.score,rgs.room_id,rgs.game_time,br.room_number from '.Room_Game_Score.
                    ' rgs left join yxx_room br on rgs.room_id=br.room_id where rgs.account_id='.$account_id.' and rgs.game_type='.$game_type.'  order by rgs.game_time desc  limit '.$page.','.$rows;
                break;
            case 3:
                $sql = 'select rgs.game_type,rgs.score,rgs.room_id,rgs.game_time,br.room_number from '.Room_Game_Score.
                    ' rgs left join sg_room br on rgs.room_id=br.room_id where rgs.account_id='.$account_id.' and rgs.game_type='.$game_type.'  order by rgs.game_time desc  limit '.$page.','.$rows;
                break;
            case 22:
                $sql = 'select rgs.game_type,rgs.score,rgs.room_id,rgs.game_time,br.room_number from '.Room_Game_Score.
                    ' rgs left join sg_room br on rgs.room_id=br.room_id where rgs.account_id='.$account_id.' and rgs.game_type='.$game_type.'  order by rgs.game_time desc  limit '.$page.','.$rows;
                break;
        }


        $query = $this->getDataBySql($dealerDB,0,$sql);
        if($query != DB_CONST::DATA_NONEXISTENT)
        {
            foreach($query as $item)
            {
                $result[] = array(
                    "game_type"=>$item['game_type'],
                    "score"=>$item['score'],
                    "room_id"=>$item['room_id'],
                    "game_time"=>$item['game_time'],
                    "room_number"=>$item['room_number']
                );
            }
        }
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取房间积分列表");
    }

    //查询最近参与的开房记录
    public function getMyRoom($arrData)
    {
        $result = array();
        if(!isset($arrData['account_id']) || $arrData['account_id'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getGameScore):lack of account_id "." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("account_id");
        }
        if(!isset($arrData['game_type']) )
        {
            log_message('error', "function(getGameScore):lack of game_type "." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("game_type");
        }
        $dealerDB = Game_CONST::DBConst_Name;

        $account_id = $arrData['account_id'];
        $game_type = $arrData['game_type'];
        $page = $arrData['page'];
        $rows = $arrData['rows'];
        $tableName = '';
        $gameFlag = false;
	    $isLaiB = false;
        switch($game_type)
        {
            //牛牛
            case 52:
            case 55:
            case 56:
            case 58:
            case 59:
                $gameFlag = true;
                $tableName = ' bull_room ';
                $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id,t.game_type from '.$tableName.'  t  where t.game_type = '.$game_type.' and t.account_id='.$account_id.'  order by t.create_time desc  limit '.$page.','.$rows;
                break;
            //三公
            case 22:
            case 3:
                $gameFlag = true;
                $tableName = ' sg_room ';
                $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id,t.game_type from '.$tableName.'  t  where t.game_type = '.$game_type.' and t.account_id='.$account_id.'  order by t.create_time desc  limit '.$page.','.$rows;
                break;
            //扎金花
            case 24:
            case 25:
            case 26:
            case 29:
            case 30:
                $gameFlag = true;
                $tableName = ' flower_room ';
                $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id,t.game_type from '.$tableName.'  t  where t.game_type = '.$game_type.' and t.account_id='.$account_id.'  order by t.create_time desc  limit '.$page.','.$rows;
                break;
            //赖子牛牛
            case 53:
                $isLaiB = true;
                $tableName = ' laibull_room ';
                $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id,t.game_type,t.max_count from '.$tableName.'  t  where t.account_id='.$account_id.'  order by t.create_time desc  limit '.$page.','.$rows;
                break;
            //斗地主
            case 2:
                $tableName = ' landlord_room ';
                $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id from '.$tableName.'  t  where t.account_id='.$account_id.'  order by t.create_time desc  limit '.$page.','.$rows;
                break;
            //麻将
            case 6:
                $tableName = ' gd_mahjong_room ';
                $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id from '.$tableName.'  t  where t.account_id='.$account_id.'  order by t.create_time desc  limit '.$page.','.$rows;
                break;
            //二八杠
            case 8:
                $tableName = ' eba_room ';
                $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id from '.$tableName.'  t  where t.account_id='.$account_id.'  order by t.create_time desc  limit '.$page.','.$rows;
                break;
            //牌九
            case 19:
                $tableName = ' paijiu_room ';
                $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id from '.$tableName.'  t  where t.account_id='.$account_id.'  order by t.create_time desc  limit '.$page.','.$rows;
                break;
            //大吃小
            case 20:
                $tableName = ' dcx_room ';
                $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id from '.$tableName.'  t  where t.account_id='.$account_id.'  order by t.create_time desc  limit '.$page.','.$rows;
                break;
            //鱼虾蟹
            case 21:
                $tableName = ' yxx_room ';
                $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id from '.$tableName.'  t  where t.account_id='.$account_id.'  order by t.create_time desc  limit '.$page.','.$rows;
                break;
            default:
                continue;
                break;
        }

//        $gameFlag = false;
//	    $isLaiB = false;
//        if($game_type == 5 || $game_type == 26 || $game_type == 29 || $game_type ==30 || $game_type == 24 || $game_type == 25 || $game_type == 3)
//        {
//            $gameFlag = true;
//            $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id,t.game_type from '.$tableName.'  t  where t.account_id='.$account_id.' order by t.create_time desc  limit '.$page.','.$rows;
//        }
//        else if($game_type == 23)
//        {
//            $isLaiB = true;
//            $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id,t.game_type,t.max_count from '.$tableName.'  t  where t.account_id='.$account_id.' order by t.create_time desc  limit '.$page.','.$rows;
//        }
//        else
//        {
//            $sql = 'select t.room_number,t.create_time,t.is_close,t.room_id from '.$tableName.'  t  where t.account_id='.$account_id.' order by t.create_time desc  limit '.$page.','.$rows;
//        }
        $query = $this->getDataBySql($dealerDB,0,$sql);
        if($query != DB_CONST::DATA_NONEXISTENT)
        {
            foreach($query as $item)
            {
                if($gameFlag)
                {
                    $result[] = array(
                        "is_close"=>$item['is_close'],
                        "room_id"=>$item['room_id'],
                        "create_time"=>$item['create_time'],
                        "room_number"=>$item['room_number'],
                        "game_type"=>$item['game_type']
                    );
                }
                else if ($isLaiB)
                {                  
                    $result[] = array(
                        "is_close"=>$item['is_close'],
                        "room_id"=>$item['room_id'],
                        "create_time"=>$item['create_time'],
                        "room_number"=>$item['room_number'],
                        "max_count"=>$item['max_count']
                    );
                }  
                else
                {
                    $result[] = array(
                        "is_close"=>$item['is_close'],
                        "room_id"=>$item['room_id'],
                        "create_time"=>$item['create_time'],
                        "room_number"=>$item['room_number']
                    );
                }

            }
        }
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取开房记录列表");
    }

    public function getRoomInfo($arrData)
    {
        $result = array();
        if(!isset($arrData['roomId']) || $arrData['roomId'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getRoomInfo):lack of account_id "." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("roomId");
        }
        if(!isset($arrData['gameType']) || $arrData['gameType'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getRoomInfo):lack of game_type "." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("gameType");
        }
        $dealerDB = Game_CONST::DBConst_Name;

        $roomId = $arrData['roomId'];
        $game_type = $arrData['gameType'];
        $sql = 'select create_time,game_type,room_id,round,game_result from '.Room_Game_Result.' where room_id='.$roomId.' and game_type='.$game_type.'  order by create_time desc  ';
        $query = $this->getDataBySql($dealerDB,0,$sql);
        if($query != DB_CONST::DATA_NONEXISTENT)
        {
            foreach($query as $item)
            {
                $result[] = array(
                    "create_time"=>$item['create_time'],
                    "game_type"=>$item['game_type'],
                    "room_id"=>$item['room_id'],
                    "round"=>$item['round'],
                    "game_result"=>$item['game_result']
                );
            }
        }
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取房间战绩详情");
    }

    public function getGameRoomInfo($arrData)
    {
        $result = array();
        if(!isset($arrData['roomId']) || $arrData['roomId'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getRoomInfo):lack of account_id "." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("roomId");
        }
        if(!isset($arrData['gameType']) || $arrData['gameType'] == G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getRoomInfo):lack of game_type "." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("gameType");
        }
        $dealerDB = Game_CONST::DBConst_Name;

        $roomId = $arrData['roomId'];
        $game_type = $arrData['gameType'];
        $sql = 'select create_time,game_type,room_id,round,game_result from '.Room_Game_Result.' where room_id='.$roomId.' and game_type='.$game_type.'  order by create_time desc  ';

        $query = $this->getDataBySql($dealerDB,0,$sql);
        $result =[];
        if($query != DB_CONST::DATA_NONEXISTENT)
        {
            foreach($query as $item)
            {
                $result[] = array(
                    "create_time"=>$item['create_time'],
                    "game_type"=>$item['game_type'],
                    "room_id"=>$item['room_id'],
                    "round"=>$item['round'],
                    "game_result"=>$item['game_result']
                );
            }
        }
        $sql9 = 'SELECT * FROM `'.Room_Scoreboard.'` WHERE `room_id` = '.$roomId .' and game_type='.$game_type;
        $query9 = $this->getDataBySql($dealerDB,1,$sql9);
        $result2 = [];
        if($query9 != DB_CONST::DATA_NONEXISTENT)
        {
            $r=json_decode($query9['balance_board'],true);

            foreach($r as $item){
                $result2[] = array(
                    "score"=>$item['score'],
                    "nickname"=>$item['name']
                );
            }
        }
        if(!empty($result2)){
            return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'scorelist'=>$result2,'result_message'=>"获取房间战绩详情");
        }

        $sql = 'SELECT rc.score, wa.nickname FROM `'.Room_Game_Score.'` rc LEFT JOIN `wechat_account` wa ON wa.`account_id` = rc.`account_id`  WHERE `room_id` = '.$roomId  .' and rc.game_type='.$game_type;
        $query2 = $this->getDataBySql($dealerDB,0,$sql);
        if($query2 != DB_CONST::DATA_NONEXISTENT)
        {
            foreach($query2 as $item)
            {
                $result2[] = array(
                    "score"=>$item['score'],
                    "nickname"=>$item['nickname']
                );
            }
        }
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'scorelist'=>$result2,'result_message'=>"获取房间战绩详情");
    }


	/*
		个人主页(带公会系统)
	*/
	public function getUserInfoGuild($arrData)
	{
		$timestamp = time();
		$result = array();
		
		if(!isset($arrData['open_id']) || $arrData['open_id'] == G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(getUserInfoGuild):lack of open_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("open_id");
		}
		
		$open_id = $arrData['open_id'];
		
		$dealerDB = Game_CONST::DBConst_Name;

		//判断open_id是否存在
		$account_where = 'open_id="'.$open_id.'"';
		$account_sql = 'select account_id,nickname,headimgurl,phone from '.WX_Account.' where '.$account_where.'';
		$account_query = $this->getDataBySql($dealerDB,1,$account_sql);
		if(DB_CONST::DATA_NONEXISTENT == $account_query)
		{
			log_message('error', "function(getUserInfoGuild):账号不存在"." in file".__FILE__." on Line ".__LINE__);
			return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"账号不存在");
		}
		else
		{
			$account_id = $account_query['account_id'];
			$nickname = $account_query['nickname'];
			$headimgurl = $account_query['headimgurl'];
			$phone = $account_query['phone'];
			if($phone == null)
			{
				$phone = "";
			}
		}
		
		//获取是否公会会长
		$level = -1;
		$invite_code = "";
		$group_id = -1;

		$member_where = 'account_id='.$account_id.' and is_delete=0';
		$member_sql = 'select group_id,level,code from '.Guild_Member.' where '.$member_where.' limit 1';
		$member_query = $this->getDataBySql($dealerDB,1,$member_sql);
		if(DB_CONST::DATA_NONEXISTENT != $member_query)
		{
			$level = $member_query['level'];
			$group_id = $member_query['group_id'];
			$invite_code = $member_query['code'];
		}
		
		$result['level'] = $level;
		$result['group_id'] = $group_id;
		$result['invite_code'] = $invite_code;

		$result['account_id'] = $account_id;
		$result['nickname'] = $nickname;
		$result['headimgurl'] = $headimgurl;
		$result['open_id'] = $open_id;
		$result['phone'] = $phone;
		
		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取用户信息");
	}
    //获取手机验证码
    public function getMobileSmsNew($arrData)
    {
        $timestamp = time();
        $result = array();
        if(!isset($arrData['phone']) || $arrData['phone'] === G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getMobileSms):lack of phone"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("phone");
        }
        $dealer_num = $arrData['dealer_num'];

        $mobile = $arrData['phone'];

        $DelaerConst = "Dealer_".$dealer_num;
        $dealerDB = Game_CONST::DBConst_Name;

        //判断open_id是否存在
        $account_phone = "";
        $account_where = 'phone="'.$mobile.'"';
        $account_sql = 'select account_id,phone from '.WX_Account.' where '.$account_where.'';
        $account_query = $this->getDataBySql($dealerDB,1,$account_sql);
        if(DB_CONST::DATA_NONEXISTENT == $account_query)
        {
            log_message('error', "function(getMobileSms):lack of dealer_num"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"手机不存在");
        }

        //判断验证码
        $sms_sql = 'select create_time from '.Sms_Detail.' where mobile="'.$mobile.'" and is_delete=0';
        $sms_query = $this->getDataBySql($dealerDB,1,$sms_sql);

        if(DB_CONST::DATA_NONEXISTENT != $sms_query)
        {
            if($sms_query['create_time'] + 120 >= $timestamp)
            {
                log_message('error', "function(getMobileSms):获取验证码频率太高：".$mobile." in file".__FILE__." on Line ".__LINE__);
                return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"获取验证码频率太高");
            }
        }
        //生成验证码
        $identifyingCode = $this->createIdentifyingCode();
        //$identifyingCode = "6666";
        log_message("error",$identifyingCode);
        $array['mobile'] = $mobile;
        $array['identifying_code'] = $identifyingCode;
        $array['type'] = 1;	//1绑定
        $array['create_time'] = $timestamp;
        $array['invaild_time'] = $timestamp + 300;
        $array['is_delete'] = G_CONST::IS_FALSE;
//        $array['extra'] = $open_id;

        $sms_id = $this->getInsertID($dealerDB,Sms_Detail, $array);

        //短信内容
        $content = "您的验证码是%identifyingCode%,验证码5分钟有效，请勿泄露他人";
        $replaceArr = array("%identifyingCode%"=>$identifyingCode);
        $content = strtr($content, $replaceArr);

        //发送短信
        $smsResult = SMS::sendSMS_aliyun($mobile,$identifyingCode);
        if($smsResult == -1)
        {
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"获取手机验证码失败");
        }

        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取手机验证码");
    }
    //获取手机验证码
    public function getMobileSms($arrData)
    {
        $timestamp = time();
        $result = array();

        if(!isset($arrData['open_id']) || $arrData['open_id'] === G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getMobileSms):lack of open_id"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("open_id");
        }
        if(!isset($arrData['dealer_num']) || $arrData['dealer_num'] === G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getMobileSms):lack of dealer_num"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("dealer_num");
        }
        if(!isset($arrData['phone']) || $arrData['phone'] === G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(getMobileSms):lack of phone"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("phone");
        }
        $dealer_num = $arrData['dealer_num'];
        $open_id = $arrData['open_id'];
        $mobile = $arrData['phone'];

        $DelaerConst = "Dealer_".$dealer_num;
        $dealerDB = Game_CONST::DBConst_Name;

        //判断open_id是否存在
        $account_phone = "";
        $account_where = 'union_id="'.$open_id.'"';
        $account_sql = 'select account_id,phone from '.WX_Account.' where '.$account_where.'';
        $account_query = $this->getDataBySql($dealerDB,1,$account_sql);
        if(DB_CONST::DATA_NONEXISTENT == $account_query)
        {
            log_message('error', "function(getMobileSms):lack of dealer_num"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"账号不存在");
        }
        else
        {
            $account_id = $account_query['account_id'];
            $account_phone = $account_query['phone'];
        }

        if($mobile == $account_phone)
        {
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"你已绑定该手机号码");
        }


        //判断手机是否已被绑定
        $bind_accountid = -1;
        $is_bind = 0;
        $mobile_where = 'phone="'.$mobile.'" and is_delete=0';
        $mobile_sql = 'select account_id from '.WX_Account.' where '.$mobile_where.'';
        $mobile_query = $this->getDataBySql($dealerDB,1,$mobile_sql);
        if(DB_CONST::DATA_NONEXISTENT != $mobile_query)
        {
            $bind_accountid = $mobile_query['account_id'];
            $is_bind = 1;
        }

        //判断验证码
        $sms_sql = 'select create_time from '.Sms_Detail.' where mobile="'.$mobile.'" and is_delete=0';
        $sms_query = $this->getDataBySql($dealerDB,1,$sms_sql);

        if(DB_CONST::DATA_NONEXISTENT != $sms_query)
        {
            if($sms_query['create_time']+300 >= $timestamp)
            {
                log_message('error', "function(getMobileSms):获取验证码频率太高：".$mobile." in file".__FILE__." on Line ".__LINE__);
                return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"获取验证码频率太高");
            }
        }
        //生成验证码
        $identifyingCode = $this->createIdentifyingCode();
        //$identifyingCode = "6666";
        log_message("error",$identifyingCode);
        $array['mobile'] = $mobile;
        $array['identifying_code'] = $identifyingCode;
        $array['type'] = 1;	//1绑定
        $array['create_time'] = $timestamp;
        $array['invaild_time'] = $timestamp + 300;
        $array['is_delete'] = G_CONST::IS_FALSE;
        $array['extra'] = $open_id;

        $sms_id = $this->getInsertID($dealerDB,Sms_Detail, $array);

        //短信内容
        $content = "您的验证码是%identifyingCode%,验证码5分钟有效，请勿泄露他人";
        $replaceArr = array("%identifyingCode%"=>$identifyingCode);
        $content = strtr($content, $replaceArr);

        //发送短信
        $smsResult = SMS::sendSMS_aliyun($mobile,$identifyingCode);
        if($smsResult == -1)
        {
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"获取手机验证码失败");
        }
        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"获取手机验证码");
    }

    public function checkSmsCodeLogin($arrData){
        $timestamp = time();
        $result = array();

        if(!isset($arrData['mobile']) || $arrData['mobile'] === G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(checkSmsCodeLogin):lack of phone"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("phone");
        }
        if(!isset($arrData['smscode']) || $arrData['smscode'] === G_CONST::EMPTY_STRING)
        {
            log_message('error', "function(checkSmsCodeLogin):lack of smscode"." in file".__FILE__." on Line ".__LINE__);
            return $this->missingPrameterArr("smscode");
        }
        $mobile = $arrData['mobile'];
        $code = $arrData['smscode'];

        $dealerDB = Game_CONST::DBConst_Name;

        //phone
        $account_where = 'phone="'.$mobile.'" and is_delete = 0';
        $account_sql = 'select account_id from '.WX_Account.' where '.$account_where.'';
        $account_query = $this->getDataBySql($dealerDB,1,$account_sql);
        if(DB_CONST::DATA_NONEXISTENT == $account_query)
        {
            log_message('error', "function(checkSmsCodeLogin):account not exist"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"账号不存在");
        }


        //判断验证码
        $sms_sql = 'select sms_id,invaild_time from '.Sms_Detail.' where mobile="'.$mobile.'" and identifying_code="'.$code.'" and type=1 and is_delete=0 order by sms_id desc';
        $sms_query = $this->getDataBySql($dealerDB,1,$sms_sql);
        if(DB_CONST::DATA_NONEXISTENT == $sms_query)
        {
            log_message('error', "function(checkSmsCodeLogin):sms not exist"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"验证码错误");
        }
        $invaild_time = $sms_query['invaild_time'];
        if($timestamp > $invaild_time)
        {
            log_message('error', "function(checkSmsCodeLogin):sms timeout"." in file".__FILE__." on Line ".__LINE__);
            return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"验证码已过期");
        }


        //删除短信记录
        $updateSms_str = 'is_delete=1';
        $updateSms_where = 'mobile="'.$mobile.'" and type=1 and is_delete=0';
        $updateSms_query = $this->changeNodeValue($dealerDB,Sms_Detail,$updateSms_str,$updateSms_where);

        $result['card_count'] = 0;
        $result['account_id'] = $account_query['account_id'];

        return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"发送成功");
    }
	//绑定新手机
	public function checkSmsCode($arrData)
	{
		$timestamp = time();
		$result = array();

		if(!isset($arrData['open_id']) || $arrData['open_id'] === G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(checkSmsCode):lack of open_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("open_id");
		}
		if(!isset($arrData['phone']) || $arrData['phone'] === G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(checkSmsCode):lack of phone"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("phone");
		}
		//if(!isset($arrData['code']) || $arrData['code'] === G_CONST::EMPTY_STRING)
		//{
		//	log_message('error', "function(checkSmsCode):lack of code"." in file".__FILE__." on Line ".__LINE__);
		//	return $this->missingPrameterArr("code");
		//}

		if(!isset($arrData['password']) || $arrData['password'] === G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(checkPassword):lack of code"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("password");
		}

		$open_id = $arrData['open_id'];
		$mobile = $arrData['phone'];
		//$code = $arrData['code'];
		$password = $arrData['password'];

		$dealerDB = Game_CONST::DBConst_Name;
		
		//判断open_id是否存在
		$account_where = 'union_id="'.$open_id.'"';
		$account_sql = 'select account_id from '.WX_Account.' where '.$account_where.'';
		$account_query = $this->getDataBySql($dealerDB,1,$account_sql);
		if(DB_CONST::DATA_NONEXISTENT == $account_query)
		{
			log_message('error', "function(checkSmsCode):account not exist"." in file".__FILE__." on Line ".__LINE__);
			return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"账号不存在");
		}
		else
		{
			$account_id = $account_query['account_id'];
		}

		//判断验证码
		//$sms_sql = 'select sms_id,invaild_time from '.Sms_Detail.' where mobile="'.$mobile.'" and identifying_code="'.$code.'" and type=1 and is_delete=0 order by sms_id desc';
		//$sms_query = $this->getDataBySql($dealerDB,1,$sms_sql);
		//if(DB_CONST::DATA_NONEXISTENT == $sms_query)
		//{
		//	log_message('error', "function(checkSmsCode):sms not exist"." in file".__FILE__." on Line ".__LINE__);
		//	return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"验证码错误");
		//}
		//$sms_id = $sms_query['sms_id'];
		//$invaild_time = $sms_query['invaild_time'];
		//if($timestamp > $invaild_time)
		//{
		//	log_message('error', "function(checkSmsCode):sms timeout"." in file".__FILE__." on Line ".__LINE__);
		//	return array('result'=>OPT_CONST::FAILED,'data'=>$result,'result_message'=>"验证码已过期");
		//}

		$account_ticket_count = 0;
		//判断手机是否已被绑定
		$return_aid = -1;
		$bind_accountid = -1;
		$is_bind = 0;
		$mobile_where = 'phone="'.$mobile.'" and is_delete=0';
		$mobile_sql = 'select account_id from '.WX_Account.' where '.$mobile_where.'';
		$mobile_query = $this->getDataBySql($dealerDB,1,$mobile_sql);
		if(DB_CONST::DATA_NONEXISTENT != $mobile_query)
		{
			$bind_accountid = $mobile_query['account_id'];
			$is_bind = 1;
		}

		//新手机
		if($is_bind == 0)
		{
			$return_aid = $account_id;
			//绑定新账号
			$bind_array['phone'] = $mobile;
			$bind_array['password'] = $password;
			$bind_query = $this->updateFunc($dealerDB,"account_id",$account_id,WX_Account,$bind_array);
		}
		else
		{
			$return_aid = $bind_accountid;

			//修改删除手机
            $delete_phone_array['phone'] = '';
			$delete_old_phone_query = $this->updateFunc($dealerDB,"phone",$mobile,WX_Account,$delete_phone_array);

			//修改当前账号绑定的手机
			$update_array['update_time'] = $timestamp;
			$update_array['update_appid'] = $open_id;
			$update_array['phone'] = $mobile;
			$update_array['password'] = $password;
			$update_query = $this->updateFunc($dealerDB,"account_id",$account_id,WX_Account,$update_array);

			//删除短信记录
			$updateSms_str = 'is_delete=1';
			$updateSms_where = 'mobile="'.$mobile.'" and type=1 and is_delete=0';
			$updateSms_query = $this->changeNodeValue($dealerDB,Sms_Detail,$updateSms_str,$updateSms_where);
		}

		$result['card_count'] = 0;
		$result['account_id'] = $return_aid;

		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"绑定成功");
	}

	/*
		设置防伪码
		参数：
			my_code : 防伪码
		返回结果：
	*/
	public function updateMyCode($arrData)
	{
		$timestamp = time();
		$result = array();

		if(!isset($arrData['account_id']) || $arrData['account_id'] == G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(updateMyCode):lack of account_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("account_id");
		}
		if(!isset($arrData['my_code']) || $arrData['my_code'] == G_CONST::EMPTY_STRING)
		{
			log_message('error', "function(updateMyCode):lack of my_code"." in file".__FILE__." on Line ".__LINE__);
			return $this->missingPrameterArr("my_code");
		}

		$account_id = $arrData['account_id'];	
		$my_code = $arrData['my_code'];

        $dealerDB = Game_CONST::DBConst_Name;	
		
		//更新红包领取状态
		$updateRE_str = 'update_time='.$timestamp.',update_appid="aid_'.$account_id.'",my_code="'.$my_code.'"';
		$updateRE_where = 'account_id='.$account_id;
		$this->changeNodeValue($dealerDB,WX_Account,$updateRE_str,$updateRE_where);

		return array('result'=>OPT_CONST::SUCCESS,'data'=>$result,'result_message'=>"设置成功");
	}

    /**
     * 短码地址生成
     * url_long 需生成短码地址
     * url_now 生成失败返回地址
     * @param $tcn
     * @return mixed
     */
    function convertUrl($tcn){
//return $tcn['url_now'];
        try {

            $url='http://api.t.sina.com.cn/short_url/shorten.json?source=2815391962&&url_long='.$tcn['url_long'];
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $file_contents = json_decode(curl_exec($ch),true);
            curl_close($ch);
            $aa = $file_contents[0];

            if(0 == $aa['type']){
                return $aa['url_short'];
            }else{
                return $tcn['url_now'];
            }
        } catch (Exception $e) {
            return $tcn['url_now'];
        }

    }


}
